<script setup lang="ts">
import { changePassword } from '@/services/user'
import { passwordRules } from '@/utils/rules'
import { showFailToast, showSuccessToast, type FormInstance } from 'vant'
import { onUnmounted, ref } from 'vue'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'

const passwordOld = ref('1234')
const passwordNew = ref('1234')

const store = useUserStore()
const router = useRouter()
const onSubmit = async () => {
  try {
    await changePassword(passwordOld.value, passwordNew.value)
    showSuccessToast('修改密码成功')
    store.delUser()
    router.push('/login')
  } catch (err) {
    console.log(err)
    showFailToast('修改密码失败')
  }
}

// 密码的可见与不可见
const isShowOld = ref(false)
const isShowNew = ref(false)

const form = ref<FormInstance>()

onUnmounted(() => {})
</script>

<template>
  <div class="login-page">
    <cp-nav-bar left-arrow title="修改密码"></cp-nav-bar>
    <!-- 头部 -->

    <!-- 表单 -->
    <van-form autocomplete="off" ref="form" @submit="onSubmit">
      <van-field
        v-model="passwordOld"
        label="旧密码"
        placeholder="请输入密码"
        :type="isShowOld ? 'text' : 'password'"
        :rules="passwordRules"
      >
        <template #button>
          <cp-icon
            :name="`login-eye-${isShowOld ? 'on' : 'off'}`"
            @click="isShowOld = !isShowOld"
            style="margin-right: 10px"
          ></cp-icon>
        </template>
      </van-field>
      <van-field
        v-model="passwordNew"
        label="新密码"
        placeholder="请输入密码"
        :type="isShowNew ? 'text' : 'password'"
        :rules="passwordRules"
      >
        <template #button>
          <cp-icon
            :name="`login-eye-${isShowNew ? 'on' : 'off'}`"
            @click="isShowNew = !isShowNew"
            style="margin-right: 10px"
          ></cp-icon>
        </template>
      </van-field>

      <div class="cp-cell">
        <van-button native-type="submit" block round type="primary"> 修改密码</van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 100px;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  border: 1px solid var(--border-color);
  border-radius: 5px;
  background-color: var(--bg-secondary);
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 20px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    background-color: var(--bg-tertiary);
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
